#include "led.h"
#include "key.h"
#include "delay.h"
#include "usart.h"
#include <string.h>
#include <stdio.h>
#include "rtc.h"

uint8_t buffer[100]            = {0};
uint8_t size                   = 0;
uint8_t receive_one_frame_flag = 0;

// 定义进入待机模式的函数
void enter_standby_mode(void)
{
    // 1. 设置深睡眠模式
    SCB->SCR |= SCB_SCR_SLEEPDEEP;

    // 3. 配置PDDS = 1，表示待机模式
    PWR->CR |= PWR_CR_PDDS;

    // 5. 使用WFI指令，进入待机模式
    __WFI();
}

int main(void)
{
    // 初始化
    USART_Init();

    // 发送字符串
    uint8_t *str = "program start!\n";
    USART_SendString(str, strlen((char *)str));

    Key_Init();
    LED_Init();
    RTC_Init();

    LED_On(LED1);


    // 判断是否有标志位，如果有就直接清除
    if (PWR->CSR & PWR_CSR_SBF)
    {
        printf("wake up from standby mode!\n");
        PWR->CR |= PWR_CR_CSBF;
    }
    if (PWR->CSR & PWR_CSR_WUF)
    {
        printf("wake up event flag is set!\n");
        PWR->CR |= PWR_CR_CWUF;
    }

    while(1)
    {
        str = "would enter standby mode!\n";
        USART_SendString(str, strlen((char *)str));
        // Delay_s(2);

        // 3. 在进入待机模式前设置闹钟
        // 设置闹钟时间为 5 秒后
        RTC_SetAlarm(5);
        enter_standby_mode();

        str = "exit from standby mode!\n";
        USART_SendString(str, strlen((char *)str));
    }
}

